﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Granite.Collections;
using System.ComponentModel;
using Granite.Eventing;
using System.Collections.Specialized;
using Granite.Commons.Tests.Mocks;
using Granite.ComponentModel;

namespace Granite.Commons.Tests.Collections
{

	//    [TestClass]
//    public class ExtendedObservableDictionaryTests
//    {


//        [TestMethod]
//        public void ExtendedObservableDictionary_ConstructorTest1()
//        {
//            var result = new ExtendedObservableDictionary<int, Notifier>();

//            var dictionaryChangedAssert = new DictionaryChangedAssert(result);

//            var notifier = new Notifier();
//            var weakNotifier = new WeakNotifier();

//            result.Add(1, notifier);
//            result.Add(2, weakNotifier);

//            dictionaryChangedAssert.Expect(NotifyDictionaryChangedAction.Add);
//            dictionaryChangedAssert.Expect(NotifyDictionaryChangedAction.Add);



//        }



//        [TestMethod]
//        public void ExtendedObservableDictionary_DictionaryChangedListenerTest()
//        {
//            var result = new ExtendedObservableDictionary<int, string>();

//            var dictionaryChangedEventQueue = new Queue<Tuple<object, DictionaryChangedEventArgs>>();
//            var dictionaryChangedListener = new DictionaryChangedListener((s, e) => dictionaryChangedEventQueue.Enqueue(Tuple.Create(s, e)));
//            result.AddHandler(dictionaryChangedListener);

//            result.Add(5, "5");
//            Assert.AreEqual(1, dictionaryChangedEventQueue.Count);
//            var event1 = dictionaryChangedEventQueue.Dequeue();
//            Assert.AreSame(result, event1.Item1);
//            Assert.AreEqual(NotifyDictionaryChangedAction.Add, event1.Item2.Action);

//            result.RemoveHandler(dictionaryChangedListener);
//            result.Add(6, "6");
//            Assert.AreEqual(0, dictionaryChangedEventQueue.Count);


//        }

//        [TestMethod]
//        public void ExtendedObservableDictionary_PropertyChangedListenerTest()
//        {
//            var result = new NotifierDictionary();
//            var propertyChangedEventQueue = new Queue<Tuple<object, PropertyChangedEventArgs>>();
//            var propertyChangedListener = new PropertyChangedListener((s, e) => propertyChangedEventQueue.Enqueue(Tuple.Create(s, e)));
//            result.AddHandler(propertyChangedListener);

//            result.Boom += 1;
//            Assert.AreEqual(1, propertyChangedEventQueue.Count);
//            var event1 = propertyChangedEventQueue.Dequeue();
//            Assert.AreSame(result, event1.Item1);
//            Assert.AreEqual("Boom", event1.Item2.PropertyName);

//            result.RemoveHandler(propertyChangedListener);
//            result.Boom += 1;
//            Assert.AreEqual(0, propertyChangedEventQueue.Count);

//        }



//        /// <summary>
//        /// This exercises the code path where we remove a listener without first adding one.
//        /// </summary>
//        [TestMethod]
//        public void ExtendedObservableDictionary_RemoveHandlerTest()
//        {
//            var result = new NotifierDictionary();
//            var dictionaryChangedEventQueue = new Queue<Tuple<object, DictionaryChangedEventArgs>>();
//            var dictionaryChangedListener = new DictionaryChangedListener((s, e) => dictionaryChangedEventQueue.Enqueue(Tuple.Create(s, e)));
//            result.RemoveHandler(dictionaryChangedListener);

//            var propertyChangedEventQueue = new Queue<Tuple<object, PropertyChangedEventArgs>>();
//            var propertyChangedListener = new PropertyChangedListener((s, e) => propertyChangedEventQueue.Enqueue(Tuple.Create(s, e)));
//            result.RemoveHandler(propertyChangedListener);


//            var itemPropertyChangedEventQueue = new Queue<Tuple<object, RelayedEventArgs<PropertyChangedEventArgs>>>();
//            var itemPropertyChangedListener = new ItemPropertyChangedListener((s, e) => itemPropertyChangedEventQueue.Enqueue(Tuple.Create(s, e)));
//            result.RemoveHandler(itemPropertyChangedListener);

//        }

//        /// <summary>
//        /// This exercises the code path where we remove a listener twice
//        /// </summary>
//        [TestMethod]
//        public void ExtendedObservableDictionary_RemoveHandlerTest2()
//        {
//            var result = new NotifierDictionary();
//            var dictionaryChangedEventQueue = new Queue<Tuple<object, DictionaryChangedEventArgs>>();
//            var dictionaryChangedListener = new DictionaryChangedListener((s, e) => dictionaryChangedEventQueue.Enqueue(Tuple.Create(s, e)));
//            result.AddHandler(dictionaryChangedListener);
//            result.RemoveHandler(dictionaryChangedListener);
//            result.RemoveHandler(dictionaryChangedListener);

//            var propertyChangedEventQueue = new Queue<Tuple<object, PropertyChangedEventArgs>>();
//            var propertyChangedListener = new PropertyChangedListener((s, e) => propertyChangedEventQueue.Enqueue(Tuple.Create(s, e)));
//            result.AddHandler(propertyChangedListener);
//            result.RemoveHandler(propertyChangedListener);
//            result.RemoveHandler(propertyChangedListener);


//            var itemPropertyChangedEventQueue = new Queue<Tuple<object, RelayedEventArgs<PropertyChangedEventArgs>>>();
//            var itemPropertyChangedListener = new ItemPropertyChangedListener((s, e) => itemPropertyChangedEventQueue.Enqueue(Tuple.Create(s, e)));
//            result.AddHandler(itemPropertyChangedListener);
//            result.RemoveHandler(itemPropertyChangedListener);
//            result.RemoveHandler(itemPropertyChangedListener);

//        }

//        [TestMethod]
//        public void ExtendedObservableDictionary_Clear()
//        {
//            var result = new NotifierDictionary();

//            result.Add(1, new Notifier());
//            result.Add(2, new Notifier());
//            result.Add(3, new Notifier());

//            var removeCount = 0;
//            int keyRemovedCount = 0;

//            var assert = new DictionaryChangedAssert(result);
//            result.ValueRemoved += (s, e) => removeCount += 1;
//            result.KeyRemoved += (s, e) => keyRemovedCount += 1;
//            result.Clear();

//            assert.Expect(NotifyDictionaryChangedAction.Reset);
//            Assert.AreEqual(3, removeCount);
//            Assert.AreEqual(3, keyRemovedCount);

//        }

//        [TestMethod]
//        public void ExtendedObservableDictionary_KeyAdded()
//        {
//            var result = new NotifierDictionary();

//            var keyAddedAssert = new ItemEventAssert<int>(result, "KeyAdded");
//            var keyRemovedAssert = new ItemEventAssert<int>(result, "KeyRemoved");

//            result.Add(1, new Notifier());
//            result.Add(2, new Notifier());
//            result.Add(3, new Notifier());

//            keyAddedAssert.Expect(1);
//            keyAddedAssert.Expect(2);
//            keyAddedAssert.Expect(3);

//            result.Remove(3);

//            keyRemovedAssert.Expect(3);

//            result.Clear();
//            keyRemovedAssert.Expect(1);
//            keyRemovedAssert.Expect(2);

//        }

//        [TestMethod]
//		public void ExtendedObservableDictionary_INotifyDictionaryChangedTest()
//		{
//			var result = new NotifierDictionary();

//			result.ItemPropertyChanged += (s, e) => { };

//			var valueAddedEventQueue = new Queue<Tuple<object, ItemEventArgs<Notifier>>>();
//			var valueRemovedEventQueue = new Queue<Tuple<object, ItemEventArgs<Notifier>>>();
//			var keyAddedEventQueue = new Queue<Tuple<object, ItemEventArgs<int>>>();
//			var keyRemovedEventQueue = new Queue<Tuple<object, ItemEventArgs<int>>>();

//			result.ValueAdded += (s, e) => valueAddedEventQueue.Enqueue(Tuple.Create(s, e));
//			result.ValueRemoved += (s, e) => valueRemovedEventQueue.Enqueue(Tuple.Create(s, e));
//			result.KeyAdded += (s, e) => keyAddedEventQueue.Enqueue(Tuple.Create(s, e));
//			result.KeyRemoved += (s, e) => keyRemovedEventQueue.Enqueue(Tuple.Create(s, e));

//			var notifier1 = new Notifier();
//			result.Add(1, notifier1);

//			Assert.AreEqual(1, valueAddedEventQueue.Count());
//			Assert.AreEqual(0, valueRemovedEventQueue.Count());
//			Assert.AreEqual(1, keyAddedEventQueue.Count());
//			Assert.AreEqual(0, keyRemovedEventQueue.Count());

//			var notifier2 = new WeakNotifier();
//			result[1] = notifier2;

//			Assert.AreEqual(2, valueAddedEventQueue.Count());
//			Assert.AreEqual(1, valueRemovedEventQueue.Count());
//			Assert.AreEqual(1, keyAddedEventQueue.Count());
//			Assert.AreEqual(0, keyRemovedEventQueue.Count());

//			result.Remove(1);

//			Assert.AreEqual(2, valueAddedEventQueue.Count());
//			Assert.AreEqual(2, valueRemovedEventQueue.Count());
//			Assert.AreEqual(1, keyAddedEventQueue.Count());
//			Assert.AreEqual(1, keyRemovedEventQueue.Count());
//		}


//        [TestMethod]
//        public void ExtendedObservableDictionary_ItemPropertyChangedListenerTest1()
//        {
//            var result = new NotifierDictionary();
//            var mutator = new Notifier();
//            result.Add(1, mutator);
//            result.Add(2, new WeakNotifier());

//            var itemPropertyChangedEventQueue = new Queue<Tuple<object, RelayedEventArgs<PropertyChangedEventArgs>>>();
//            var itemPropertyChangedListener = new ItemPropertyChangedListener((s, e) => itemPropertyChangedEventQueue.Enqueue(Tuple.Create(s, e)));
//            var itemPropertyChangedAssert = new ItemPropertyChangedEventAssert(result);


//            result.AddHandler(itemPropertyChangedListener);
//            mutator.Name = "AAA";

//            Assert.AreEqual(1, itemPropertyChangedEventQueue.Count);

//            var event1 = itemPropertyChangedEventQueue.Dequeue();
//            Assert.AreSame(result, event1.Item1);
//            Assert.AreEqual(mutator, event1.Item2.OriginalSender);
//            Assert.AreEqual("Name", event1.Item2.EventArgs.PropertyName);

//            itemPropertyChangedAssert.Expect(mutator, "Name");


//            result.RemoveHandler(itemPropertyChangedListener);

//            mutator.Name = "BBB";

//            Assert.AreEqual(0, itemPropertyChangedEventQueue.Count);

//        }
//    }

}
